package com.ljy.tool;

import com.ljy.utils.Doc2;
import com.ljy.utils.Write;
import org.apache.http.client.utils.URIBuilder;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.URISyntaxException;

public class Search {
    /*public boolean searchInCsdn(String str){
        return searchInCsdn(str,"gbk");
    }
    public boolean searchInCsdn(String str,String code){
        try {
            StringBuilder stringBuilder = new StringBuilder();
            String url = "https://so.csdn.net/so/search";
            Document document;

            if (str != null && str.length() > 0) {
                URIBuilder uriBuilder = new URIBuilder(url);
                uriBuilder.setParameter("q", str);
                URI build = uriBuilder.build();
                System.out.println(build.toString());
                document = new Doc2().getDocument(build, code);
            } else {
                document = new Doc2().getDocument(url, code);
            }
            System.out.println("html"+document.html());
            Elements as = document.select("#app>div>div" +
                    ">div>div>div>div>div>div>h3>a");

            int i = 0;
            for (Element a : as) {
                System.out.println(i++);
                System.out.println(a.text() + "\n" + a.attr("href"));

                stringBuilder.append(i).append("\r\n").
                        append(a.text() + "\r\n" + a.attr("href") + "\r\n");
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
            return true;
    }
    */
    public boolean searchInBaiduWenku(String str) {
        return searchInBaiduWenku(str, "gbk");
    }

    public boolean searchInBaiduWenku(String str, String code) {
        try {
            StringBuilder stringBuilder = new StringBuilder();
            String url = "https://wenku.baidu.com/search";
            Document document;

            if (str != null && str.length() > 0) {
                URIBuilder uriBuilder = new URIBuilder(url);
                uriBuilder.setParameter("word", str);
                document = new Doc2().getDocument(uriBuilder.build(), code);
            } else {
                System.out.println("什么也不搜！");
                document = new Doc2().getDocument(url, code);
            }
            Elements as = document.select("div>dl>dt>p>a");
            int i = 0;
            for (Element a : as) {
                System.out.println(i++);
                System.out.println(a.attr("title") + "\n" + a.attr("href"));

                stringBuilder
                        .append(i).append("\r\n").
                        append(a.attr("title")).append("\r\n")
                        .append(a.attr("href")).append("\r\n");
            }

            for (int j = 0; j < 10; j++) {
                Element next = document.select("a.next").first();
                System.out.println("第" + (j + 2) + "页");
                if (next != null) {
                    System.out.println("https://wenku.baidu.com/search" +
                            next.attr("href"));
                    url = "https://wenku.baidu.com/search" + next.attr("href");
                    Elements nexts = new Doc2().getDocument(url, code).select("div>dl>dt>p>a");
                    for (Element element : nexts) {
                        System.out.println("第" + (j + 2) + "页" + (i++));
                        System.out.println(element.attr("title") + "\n" + element.attr("href"));

                        stringBuilder.append("第" + (j + 2) + "页" + i + "\r\n")
                                .append(element.attr("title") + "\r\n" + element.attr("href") + "\r\n");
                    }
                } else break;
            }
            String s = stringBuilder.toString();
            new Write().write(s, "C:\\Users\\AikeTech\\Desktop\\WebCrawler\\" + str + ".txt", true);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
